'use strict';
const { getDefaultModelConfig } = require("../../utils/common");
const {formatDecimal, formatDate} = require("../../utils");

module.exports = app => {
    const { STRING, BIGINT, TEXT, TINYINT, DECIMAL,DATE } = app.Sequelize;
    const OrderCode = app.model.define(
        'OrderCode',
        {
            id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键ID' },
            shop_user_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '店铺用户ID' },
            order_no: STRING(255),
            user_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '用户ID' },
            parent_product_name: { type: STRING(255), allowNull: true, comment: '父产品名称' },
            product_name: { type: STRING(255), allowNull: false, comment: '产品名称' },
            product_price: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('product_price'))
                },
            },
            product_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '产品ID' },
            parent_product_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '父产品ID' },
            order_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '订单ID' },
            order_item_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '订单项ID' },
            code: { type: STRING(64), allowNull: false, unique: 'idx_code', comment: '核销码' },
            code_url: TEXT,
            product_detail: TEXT,
            status: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '状态（1已核销、0未核销）' },
            // 返利金额
            rebate_price: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price'))
                },
            },
            // 平台收入
            rebate_price_platform: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_platform'))
                },
            },
            // 百分比
            rebate_price_platform_percent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_platform_percent'))
                },
            },
            // 商家收入
            rebate_price_shop: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_shop'))
                },
            },
            // 分成比例
            rebate_price_shop_percent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_shop_percent'))
                },
            },
            // 代理收入
            rebate_price_agent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent'))
                },
            },
            // 分成比例
            rebate_price_agent_percent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent_percent'))
                },
            },
            // 代理收入
            rebate_price_agent2: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent2'))
                },
            },
            // 分成比例
            rebate_price_agent_percent2: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent_percent2'))
                },
            },
            // 核销时间
            verify_time: {
                type: DATE,
                defaultValue: null,
                comment: '创建时间',
                get() {
                    return formatDate(this.getDataValue('verify_time'), 'YYYY-MM-DD HH:mm:ss');
                },
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'order_code',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time',
            comment: '订单核销码表',
            indexes: [
                {
                    unique: true,
                    fields: ['code'],
                    name: 'idx_code'
                }
            ]
        }
    );
    OrderCode.associate = function () {
        // 关联用户
        OrderCode.belongsTo(app.model.Business.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user'
        })
    }
    return OrderCode
};
